package com.practise.rabbitmqdemo.listen;

import com.practise.rabbitmqdemo.config.TTLDirectRabbitConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class TTLDirectReceiver {


    @RabbitListener(queues = TTLDirectRabbitConfig.TTL_Q1,ackMode ="MANUAL")
    public void processQueue11(String msg , Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        try {
            // 处理实际业务
            // 制造异常
            // int wrongNumber = 1/0;
            // 无异常，确认消息消费成功
            channel.basicAck(deliveryTag, true);
            log.info(" TTL实现 deliveryTag: {} 监听{} msg={}",deliveryTag, TTLDirectRabbitConfig.TTL_Q1,msg);
        }catch (IOException | ArithmeticException exception) {
            // 有异常，将消息返回给Queue里，第三个参数requeue可以直接看出来，是否返回到Queue中
            System.out.println("TTL实现  有异常  ");
            channel.basicNack(deliveryTag, true, true);
        }
    }
}